package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
@Slf4j
@CrossOrigin
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public R list() {
        log.info("查询全部部门");
        List<Dept> list = deptService.list();
        return R.ok(list);
    }
    @PostMapping
    public R  add( @RequestBody Dept dept){
        log.info("添加部门:{}",dept);
        deptService.add(dept);
        return R.ok();

    }
    @DeleteMapping
    public  R deleteById(Integer id){
        log.info("删除部门:{}",id);
        deptService.delectById(id);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R GetById(@PathVariable Integer id){
        log.info("根据id查询:{}",id);
       Dept dept= deptService.GetById(id);
       return R.ok(dept);
    }


    @PutMapping
    public R update(@RequestBody Dept dept){
        log.info("修改部门{}",dept);
        deptService.update(dept);
        return R.ok();
    }

}
